package com.wteam.dragon.system.apps.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.system.apps.server.povo.bean.QiniuConfig;
import com.wteam.dragon.system.apps.server.povo.bean.QiniuContent;
import com.wteam.dragon.system.apps.server.service.QiniuService;
import com.wteam.dragon.system.apps.server.povo.vo.QiNiuConfigVo;
import com.wteam.dragon.system.apps.server.povo.vo.QiNiuContentVo;
import com.wteam.dragon.system.basebean.bean.BaseBean;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

/**
 * @Author 陈晓辉
 * @Date 2020/3/24
 */
@RestController
@RequestMapping("admin/qiniu")
@Api(tags = "系统：七牛云")
@RequiredArgsConstructor
public class QiniuController extends BaseBean {
    private final QiniuService qiniuService;

    @PostMapping("addQiniuConfig")
    @ApiOperation("添加七牛云配置")
    @PreAuthorize("@el.check('addQiniuConfig')")
    public ResultMessage addQiniuConfig(QiNiuConfigVo qiniuConfigVo){
        QiniuConfig qiniuConfig = BeanUtil.from(qiniuConfigVo, QiniuConfig.class);
        return qiniuService.addQiniuConfig(qiniuConfig);
    }

    @DeleteMapping("delQiniuConfig")
    @ApiOperation("删除七牛云配置")
    @PreAuthorize("@el.check('delQiniuConfig')")
    public ResultMessage delQiniuConfig(Long id){
        return qiniuService.delQiniuConfig(id);
    }

    @PutMapping("editQiniuConfig")
    @ApiOperation("更新七牛云配置")
    @PreAuthorize("@el.check('editQiniuConfig')")
    public ResultMessage editQiniuConfig(QiNiuConfigVo qiniuConfigVo){
        QiniuConfig qiniuConfig = BeanUtil.from(qiniuConfigVo, QiniuConfig.class);
        return qiniuService.editQiniuConfig(qiniuConfig);
    }

    @GetMapping("listQiniuConfig")
    @ApiOperation("获得七牛云配置列表")
    @PreAuthorize("@el.check('listQiniuConfig')")
    public ResultMessage listQiniuConfig(){
        return qiniuService.listQiniuConfig();
    }

    @PostMapping("uploadFile")
    @ApiOperation("上传文件")
    @PreAuthorize("@el.check('uploadFile')")
    public ResultMessage uploadFile(MultipartFile file, QiNiuConfigVo qiniuConfigVo){
        QiniuConfig qiniuConfig = BeanUtil.from(qiniuConfigVo, QiniuConfig.class);
        return qiniuService.uploadFile(file, qiniuConfig);
    }

    @GetMapping("downloadFile")
    @ApiOperation("下载文件")
    @PreAuthorize("@el.check('downloadFile')")
    public ResultMessage downloadFile(QiNiuConfigVo qiniuConfigVo, QiNiuContentVo qiniuContentVo){
        QiniuConfig qiniuConfig = BeanUtil.from(qiniuConfigVo, QiniuConfig.class);
        QiniuContent qiniuContent = BeanUtil.from(qiniuContentVo, QiniuContent.class);
        return qiniuService.downloadFile(qiniuConfig, qiniuContent);
    }

    @DeleteMapping("delFile")
    @ApiOperation("批量删除文件")
    @PreAuthorize("@el.check('delFile')")
    public ResultMessage delFile(QiNiuConfigVo qiniuConfigVo, Set<Long> qiniuContentIds){
        QiniuConfig qiniuConfig = BeanUtil.from(qiniuConfigVo, QiniuConfig.class);
        return qiniuService.delFile(qiniuConfig, qiniuContentIds);
    }

    @GetMapping("pageQiniuContent")
    @ApiOperation("分页获得图片文件")
    @PreAuthorize("@el.check('pageQiniuContent')")
    public ResultMessage pageQiniuContent(PageVo<QiniuContent> pageVo){
        Page<QiniuContent> page = BeanUtil.fromPage(pageVo);
        return qiniuService.pageQiniuContent(page);
    }

}
